//
//  ZGL24ClassifiedVC.m
//  Faker
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 dllo. All rights reserved.
//
#import <MJRefresh.h>
#import "ZGLHTTPTOOL.h"
#import "ZGL24ClassifiedVC.h"
#import "ZGL24ClassifiedCell.h"
#import "ZGL24ClassifiedModel.h"
#import "ZGL24ClsDetailVC.h"
#import "ZGLHotDetailsVC.h"
@interface ZGL24ClassifiedVC ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSArray *modArr;
@property(nonatomic, assign)NSInteger cellheight;
@end

@implementation ZGL24ClassifiedVC
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _cellheight = 44;
        [self createTableView];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.view.backgroundColor = backColor;
    _tableView.backgroundColor = backColor;
}
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)createTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, Width, Height-64) style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [self.tableView registerClass:[ZGL24ClassifiedCell class] forCellReuseIdentifier:@"ZGL24ClassifiedCell"];
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    __weak typeof(self) weakSelf = self;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf handelData];
    }];
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf handelData];
    }];
    [_tableView.header beginRefreshing];
}

- (void)handelData{
    NSString *urlStr = @"http://c.3g.163.com/nc/topicset/subscribe/recommend.html";
    __weak typeof(self) weakSelf = self;
    [ZGLHTTPTOOL GETWithURL:urlStr withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        weakSelf.modArr = [[NSArray alloc]initWithArray:[tempDic objectForKey:@"recommendlist"]];
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    } withFail:^(NSError *error) {
        NSLog(@"没网:ZGL24classifildVC");
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGL24ClassifiedModel *mod = [ZGL24ClassifiedModel modWithDic:_modArr[indexPath.row]];
    ZGL24ClassifiedCell *cell = [tableView dequeueReusableCellWithIdentifier: @"ZGL24ClassifiedCell"];
    cell.mod = mod;
    cell.buttonClick = ^(NSString *viewid, NSString *type, NSString *title){
        if ([type isEqualToString:@"tid"]) {
            ZGL24ClsDetailVC *details = [[ZGL24ClsDetailVC alloc]init];
            details.tid = viewid;
            details.title = title;
            [self.navigationController pushViewController:details animated:YES];
        }else{
            ZGLHotDetailsVC *hotDetails = [[ZGLHotDetailsVC alloc]init];
            hotDetails.docid = viewid;
            [self.navigationController pushViewController:hotDetails animated:YES];
        }
    };
    self.cellheight = cell.height;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellheight;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    self.view.userInteractionEnabled = NO;

//    self.view.userInteractionEnabled = YES;
//}

- (void)dealloc{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
